package com.lms.vo;

import com.lms.bean.Book;
import com.lms.bean.RentRecord;
import org.springframework.beans.BeanUtils;

/**
 * 视图层对象(View Object)
 * 用于传输到前端页面
 * 为了避免转化成JSON时陷入无穷递归链，所有的的视图层对象(VO)中都不能包含对象字段(例如Book,User)
 */

public class RentRecordVO extends RentRecord {
    private Long userId;
    private Long bookId;
    //private Long bookSumNumber;
    private String bookName;
    private String username;

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookname) {
        this.bookName = bookname;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * 将 Bean 转化成 VO
     */

    public static RentRecordVO convert(RentRecord rentRecord) {
        RentRecordVO rentRecordVO = new RentRecordVO();
        BeanUtils.copyProperties(rentRecord, rentRecordVO);

        //用 bookId  bookSumNumber 两个字段替换原本的 book 对象
        rentRecordVO.setUser(null);
        Book book = rentRecord.getBook();
        if(null!= book){
            rentRecordVO.setBookId(book.getBookId());
            rentRecordVO.setBookName(book.getBookName());
            //rentRecordVO.setBookSumNumber(book.getBookSumNumber());
        }

        //用 userId  这个字段替换原本的 user 对象
        rentRecordVO.setBook(null);
        if(null!=rentRecord.getUser()){
            rentRecordVO.setUserId(rentRecord.getUser().getUserId());
            rentRecordVO.setUsername((rentRecord.getUser().getUsername()));
        }
        return rentRecordVO;
    }

    /*public Long getBookSumNumber() {
        return bookSumNumber;
    }

    public void setBookSumNumber(Long bookSumNumber) {
        this.bookSumNumber = bookSumNumber;
    }*/

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getBookId() {
        return bookId;
    }

    public void setBookId(Long bookId) {
        this.bookId = bookId;
    }

}


